﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxGridView;

namespace TireService.Web.UserControls
{
    public partial class ucEditCustomerService : System.Web.UI.UserControl
    {
        private string _controlname = "EditCustomerService";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["UserControlLoaded" + _controlname]))
            {
                // Initiate datalayer
                TireServiceDB db = new TireServiceDB();

                // Show customer service
                LoadCustomerService(Convert.ToInt32(Session["CustomerId" + _controlname]), db);
                LoadCustomerTask(Convert.ToInt32(Session["CustomerId" + _controlname]), db);
            }
        }

        protected void callbackEditCustomerServicePanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            Session["CustomerId" + _controlname] = Convert.ToInt32(e.Parameter);

            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int CustomerId)
        {
            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            if (CustomerId != 0)
            {
                // Show customer service
                LoadCustomerService(CustomerId, db);
                LoadCustomerTask(CustomerId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Kunde ikke fundet");
            }

            // Dispose connection to database
            db.Dispose();
        }

        /// <summary>
        /// Function that bind customer servicedrives to gridview
        /// </summary>
        /// <param name="CustomerId">Id for customer</param>
        /// <param name="db">Connection to database</param>
        private void LoadCustomerService(int CustomerId, TireServiceDB db)
        {
            // Load planned/past service details
            gvService.DataSource = db.GetCustomerService(CustomerId, cbShowChildServices.Checked);
            gvService.DataBind();

            Session["UserControlLoaded" + _controlname] = true;
        }

        /// <summary>
        /// Function that bind customer tasks to gridview
        /// </summary>
        /// <param name="CustomerId">Id for customer</param>
        /// <param name="db">Connection to database</param>
        private void LoadCustomerTask(int CustomerId, TireServiceDB db)
        {
            // Load planned/past task details
            gvTask.DataSource = db.GetTasksByCustomer(CustomerId, cbShowChildServices.Checked);
            gvTask.DataBind();

            Session["UserControlLoaded" + _controlname] = true;
        }

        /// <summary>
        /// Postback function that updates gridview with services
        /// </summary>
        protected void gvService_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Show servicedrives
            LoadCustomerService(Convert.ToInt32(Session["CustomerId" + _controlname]), db);
        }

        /// <summary>
        /// Postback function that updates gridview with tasks
        /// </summary>
        protected void gvTask_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Show tasks
            LoadCustomerTask(Convert.ToInt32(Session["CustomerId" + _controlname]), db);
        }

        protected void gvTaskDetail_DataSelect(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();
            ASPxGridView gvTaskDetail = (sender as ASPxGridView);
            gvTaskDetail.DataSource = db.GetTaskDetailsExtendedView(Convert.ToInt32((sender as ASPxGridView).GetMasterRowKeyValue()));
        }
    }
}